<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.search"
        placeholder="全局搜索"
        style="width:200px;"
          size="mini"
        class="filter-item"
      ></el-input>
      <el-select
        v-model="listQuery.type"
        placeholder="类型"
        clearable
        style="width:90px;"
        size="mini"
        class="filter-item"
      >
        <el-option v-for="item in this.basicsType" :key="item" :label="item" :value="item"></el-option>
      </el-select>
      <el-select
        v-model="listQuery.status"
        placeholder="状态"
        clearable
        style="width:90px;"
        size="mini"
        class="filter-item"
      >
        <el-option v-for="item in this.articleStatus" :key="item" :label="item" :value="item"></el-option>
      </el-select>

      <el-button-group>
        <el-button
          v-waves
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleFilter"
        >搜索</el-button>
        <el-button
          v-waves
          class="filter-item"
          type="primary"
          icon="el-icon-edit"
          size="mini"
          @click="handleReset"
        >重置</el-button>
        <el-button
          v-waves
          class="filter-item"
          type="primary"
          size="mini"
          icon="el-icon-edit"
          @click="handleCreate"
        >添加</el-button>
      </el-button-group>
    </div>
    <el-table
      v-loading="loadingTableVisible"
      :element-loading-text="loadingTableTitle"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(0, 0, 0, 0.8)"
      :data="list"
      border
      size="mini"
    >
      <el-table-column label="编号" prop="id" align="center" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="标题" prop="title" align="center">
        <template slot-scope="scope">
          <span>{{scope.row.title}}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" prop="type" align="center" width="80">
        <template slot-scope="scope">
          <span>{{ basicsType[scope.row.type] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status" align="center" width="80">
        <template slot-scope="scope">
          <span>{{ articleStatus[scope.row.status] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="排序" prop="sort" align="center" width="160">
        <template slot-scope="{row}">
          <el-input-number
            size="mini"
            v-model="row.sort"
            :min="0"
            :max="100"
            @change="handleChange(row)"
          ></el-input-number>
        </template>
      </el-table-column>
      <el-table-column label="创建日期" width="150px" align="center">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span>{{ scope.row.creationTime | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" class="list-mini-btn" @click="handleUpdate(row)">编辑</el-button>
          <el-button type="danger" class="list-mini-btn" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--创建与编辑窗口-->
    <el-dialog :visible.sync="dialogFormVisible" :title="dialogFormTitle" width="40%">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="70px"
        style="width:80%;margin-left:50px;"
      >
        <el-form-item label="类型" prop="type">
          <el-select v-model="temp.type" placeholder="类型">
            <el-option
              v-for="(item,index) in this.basicsType"
              :key="item"
              :label="item"
              :value="index"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="temp.title"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="temp.sort" :min="1" :max="100" label="排序"></el-input-number>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input type="textarea" v-model="temp.description"></el-input>
        </el-form-item>
        <el-form-item label="图片" prop="images">
          <el-upload
            class="upload"
            action="api/file/uploadimage"
            :on-success="handleSuccess"
            :on-remove="handleRemove"
            :file-list="fileList"
            list-type="picture"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="saveData()">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import waves from "@/directive/waves"; // waves directive
import {
  getSettingList,
  getSettingInfo,
  saveSettingInfo,
  deleteSettingInfo,
  release,
  sort
} from "@/api/gateway/basics";
import { getEnum } from "@/utils/global";
export default {
  name: "clientSetting",
  directives: { waves },
  data() {
    return {
      listQuery: {
        search: "",
        type: "",
        status: ""
      },
      list: [],
      total: 0,
      loadingTableVisible: true,
      loadingTableTitle: "拼命加载中",
      temp: {
        id: undefined,
        title: "",
        type: null,
        images: [],
        sort: 1,
        description: ""
      },
      fileList: [],
      basicsType: null,
      articleStatus: null,
      dialogFormVisible: false,
      dialogFormTitle: "",
      rules: {
        type: [{ required: true, message: "类型不能为空", trigger: "change" }],
        title: [{ required: true, message: "标题不能为空", trigger: "blur" }]
      }
    };
  },
  created() {
    this.basicsType = getEnum("saaS.Entity.Enums.BasicsType");
    this.articleStatus = getEnum("saaS.Entity.Enums.ArticleStatus");
    this.getSettings();
  },
  methods: {
    getSettings() {
      this.loadingTableVisible = true;
      getSettingList(this.listQuery)
        .then(response => {
          this.list = response.data.items;
          this.total = response.data.total;
          this.loadingTableVisible = false;
          this.loadingTableTitle = "拼命加载中";
        })
        .catch(() => {
          this.loadingTableVisible = false;
          this.loadingTableTitle = "拼命加载中";
        });
    },
    //搜索事件
    handleFilter() {
      this.loadingTableTitle = "正在搜索";
      this.getSettings();
    },
    //添加事件
    handleCreate() {
      this.dialogFormVisible = true;
      this.dialogFormTitle = "添加配置信息";
    },
    //修改事件
    handleUpdate(row) {
      this.dialogFormVisible = true;
      this.dialogFormTitle = "修改配置信息";
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
      getSettingInfo(row.id).then(response => {
        this.temp = response.data;
        this.convertFileList();
      });
    },
    //转换前端展示格式
    convertFileList() {
      this.fileList = [];
      for (var item in this.temp.images) {
        var url = this.temp.images[item];
        this.fileList.push({
          name: url,
          url: "http://image.lbanyun.com/" + url
        });
      }
    },
    //删除事件
    handleDelete(row) {
      this.$confirm("确定删除配置信息？", "提示", {
        confirmButtonTest: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async () => {
        const loading = this.$loading({ text: "正在删除数据" });
        deleteSettingInfo(row.id)
          .then(response => {
            loading.close();
            this.$notify({
              title: response.success ? "成功" : "失败",
              message: response.message,
              type: response.success ? "success" : "error",
              duration: 2000
            });
            this.getSettings();
          })
          .catch(error => {
            loading.close();
          });
      });
    },
    //重置数据模型
    resetTemp() {
      this.temp = {
        id: undefined,
        title: "",
        type: null,
        images: [],
        sort: 1,
        description: ""
      };
    },
    //修改排序
    handleChange(row) {
      sort(row.id, row.sort);
    },
    //保存数据
    saveData() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          const loading = this.$loading({ text: "正在保存数据" });
          saveSettingInfo(this.temp)
            .then(response => {
              loading.close();
              this.dialogFormVisible = false;
              this.resetTemp();
              this.$nextTick(() => {
                this.$refs["dataForm"].clearValidate();
              });
              this.$notify({
                title: response.success ? "成功" : "失败",
                message: response.message,
                type: response.success ? "success" : "error",
                duration: 2000
              });
              this.getSettings();
            })
            .catch(error => {
              console.info(error);
              loading.close();
            });
        }
      });
    },
    handleRemove(file, fileList) {
      var index = this.temp.images.indexOf(file.url);
      var images = [];
      for (var item in this.temp.images) {
        if (item != index) {
          images.push(this.temp.images[item]);
        }
      }
      this.temp.images = images;
      console.log(file, fileList);
    },
    handleSuccess(response, file, fileList) {
      this.temp.images.push(response.data[0]);
      console.log(file);
    }
  }
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 278px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 278px;
  height: 178px;
  display: block;
}
</style>